package com.mostromas.ess.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mostromas.ess.entities.*;
import com.mostromas.ess.product.mapper.ProductMapper;
import com.mostromas.ess.product.req.ProductREQ;
import com.mostromas.ess.product.service.*;
import com.mostromas.ess.util.base.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    @Autowired
    private IProductStyleService productStyleService;

    @Autowired
    private IProductColorService productColorService;

    @Autowired
    private IProductSizeService productSizeService;

    @Autowired
    private IProductPriceService productPriceService;

    @Autowired
    private IFabricService fabricService;

    @Override
    public Result queryPage(ProductREQ req) {

        IPage<Product> data = baseMapper.selectPage(req.getPage(),req);
        return Result.ok(data);
    }

    @Override
    public Result findDetails(String id) {

        System.out.println(fabricService.findDetailsByProductId(id));
        return Result.ok(baseMapper.findDetails(id));


    }

    @Transactional
    @Override
    public boolean saveProduct(Product product) {
        List<ProductColor> colorList = product.getColorList();
        List<Fabric> fabricList = product.getFabricList();
        List<ProductSize> sizeList = product.getSizeList();
        List<ProductStyle> styleList = product.getStyleList();
        ProductPrice price = product.getPrice();
        super.save(product);

        baseMapper.saveProductRelationColor(product.getId(),colorList);

        baseMapper.saveProductRelationFabric(product.getId(),fabricList);

        baseMapper.saveProductRelationStyle(product.getId(),styleList);

        baseMapper.saveProductRelationSize(product.getId(),sizeList);

        price.setProductId(product.getId());
        productPriceService.save(price);

        return false;
    }

    @Transactional
    @Override
    public boolean updateProduct(Product product) {
        baseMapper.deleteProductRelationColor(product.getId());
        baseMapper.deleteProductRelationFabric(product.getId());
        baseMapper.deleteProductRelationStyle(product.getId());
        baseMapper.deleteProductRelationSize(product.getId());

        super.updateById(product);
        baseMapper.saveProductRelationColor(product.getId(),product.getColorList());

        baseMapper.saveProductRelationFabric(product.getId(),product.getFabricList());

        baseMapper.saveProductRelationStyle(product.getId(),product.getStyleList());

        baseMapper.saveProductRelationSize(product.getId(),product.getSizeList());
        productPriceService.updateById(product.getPrice());
        return false;
    }

    @Transactional
    @Override
    public boolean deleteProduct(String id) {
        QueryWrapper<ProductPrice> wrapper = new QueryWrapper<>();
        wrapper.eq("product_id",id);
        baseMapper.deleteProductRelationColor(id);
        baseMapper.deleteProductRelationFabric(id);
        baseMapper.deleteProductRelationStyle(id);
        baseMapper.deleteProductRelationSize(id);
        productPriceService.remove(wrapper);
        baseMapper.deleteById(id);
        return false;
    }

}
